﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CarLoanSystem
{
    public partial class panelEmployee : UserControl
    {
        public panelEmployee()
        {
            InitializeComponent();
            this.gvEmpInfo.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        private void genGridview()
        {
            // gen Grideview
            gvEmpInfo.AutoGenerateColumns = false;

            //Set Columns Count
            gvEmpInfo.ColumnCount = 6;

            gvEmpInfo.Columns[0].Width = 40;

            //Add Columns
            gvEmpInfo.Columns[1].Name = "EMPLOYEE_ID";
            gvEmpInfo.Columns[1].HeaderText = "รหัส";
            gvEmpInfo.Columns[1].DataPropertyName = "EMPLOYEE_ID";
            gvEmpInfo.Columns[1].Width = 35;

            gvEmpInfo.Columns[2].Name = "EMPLOYEE_NAME";
            gvEmpInfo.Columns[2].HeaderText = "ชื่อ";
            gvEmpInfo.Columns[2].DataPropertyName = "EMPLOYEE_NAME";
            gvEmpInfo.Columns[2].Width = 100;

            gvEmpInfo.Columns[3].Name = "EMPLOYEE_LASTNAME";
            gvEmpInfo.Columns[3].HeaderText = "นามสกุล";
            gvEmpInfo.Columns[3].DataPropertyName = "EMPLOYEE_LASTNAME";
            gvEmpInfo.Columns[3].Width = 115;

            gvEmpInfo.Columns[4].Name = "Display";
            gvEmpInfo.Columns[4].HeaderText = "ตำแหน่ง";
            gvEmpInfo.Columns[4].DataPropertyName = "Display";
            gvEmpInfo.Columns[4].Width = 150;

            gvEmpInfo.Columns[5].Name = "EMPLOYEE_GROUP";
            gvEmpInfo.Columns[5].HeaderText = "กลุ่มงาน";
            gvEmpInfo.Columns[5].DataPropertyName = "EMPLOYEE_GROUP";
            gvEmpInfo.Columns[5].Width = 75;
            // end gen gridview

            gvEmpInfo.DataSource = LoadEmployee();
        }

        private DataTable LoadEmployee()
        {
            DataTable dt = DAL.EmployeeInfoDAL.LoadAllEmployee(this.txtEmployeeID.Text, this.txtName.Text, this.txtLastName.Text);

            dt.Columns.Add("Display", typeof(string));

            foreach (DataRow dr in dt.Rows)
            {
                if (dr[6].ToString().Equals("a"))
                {
                    dr[9] = "ผู้ดูแลระบบ";
                }
                else if (dr[6].ToString().Equals("h"))
                {
                    dr[9] = "หัวหน้าพนักงานปล่อยสินเชื่อ";
                }
                else if (dr[6].ToString().Equals("s"))
                {
                    dr[9] = "พนักงานปล่อยสินเชื่อ";
                }
                else if (dr[6].ToString().Equals("p"))
                {
                    dr[9] = "พนักงานรับชำระเงิน";
                }
                else if (dr[6].ToString().Equals("l"))
                {
                    dr[9] = "พนักงานกฎหมาย";
                }
                else if (dr[6].ToString().Equals("d"))
                {
                    dr[9] = "พนักงานทวงหนี้สิน";
                }
                else if (dr[6].ToString().Equals("b"))
                {
                    dr[9] = "ผู้บิรหาร";
                }
            }
            return dt;
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                if (((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value != null && !((DataGridView)sender).Rows[e.RowIndex].Cells[2].Value.ToString().Equals(string.Empty))
                {
                    frmEditEmployee frmEditEmployee = new frmEditEmployee(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString());
                    frmEditEmployee.ShowDialog();
                }
                else
                {
                    gvEmpInfo.DataSource = LoadEmployee();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmEditEmployee frmEditEmployee = new frmEditEmployee();
            frmEditEmployee.ShowDialog();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            gvEmpInfo.DataSource = LoadEmployee();
        }

    }
}
